using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CollisionManager : MonoBehaviour {
	
	private ProtectedObject PO_script;
	void Awake()
	{
		
		PO_script = GameManager.getInstance().TargetObject.GetComponent<ProtectedObject>();
		
		if( null == Instance )
		{
			Instance = FindObjectOfType(typeof ( CollisionManager ) ) as CollisionManager;
			if( null != Instance )
			{
				Instance.InitCollisionManager();
			}
		}
		if( null == Instance )
		{
			GameObject obj = new GameObject("CollisionManager");
			Instance = obj.AddComponent(typeof ( CollisionManager ) ) as CollisionManager;
			Instance.InitCollisionManager();
		}
	}
	private static CollisionManager Instance = null;
	public static CollisionManager getInstance()
	{
		if( null == Instance )
		{
			Instance = FindObjectOfType(typeof ( CollisionManager ) ) as CollisionManager;
			if( null != Instance )
			{
				Instance.InitCollisionManager();
			}
		}
		if( null == Instance )
		{
			GameObject obj = new GameObject("CollisionManager");
			Instance = obj.AddComponent(typeof ( CollisionManager ) ) as CollisionManager;
			Instance.InitCollisionManager();
		}
		
		return Instance;
	}
	private void InitCollisionManager()
	{
	}
	public void Request_Attack(float _damage)
	{
		try
		{
			foreach( GameObject obj in MonsterManager.getInstance().MonsterList)
			{
				if(CrashCheck(obj.GetComponent<Monster>(),GameManager.getInstance().Player_Object.transform.position))
				{
					obj.GetComponent<Monster>().WasAttacked(_damage);
				}
			}

		}
		catch
		{
			
		}
	}
	public void Request_Attack(float _damage,Vector3 _pos)
	{
		foreach( GameObject obj in MonsterManager.getInstance().MonsterList)
		{
			if(CrashCheck(obj.GetComponent<Monster>(),_pos))
			{
				obj.GetComponent<Monster>().WasAttacked(_damage);
			}
		}

	}
	public bool IsCrashed()
	{
		foreach( GameObject obj in MonsterManager.getInstance().MonsterList)
		{
			if(CrashCheck(obj.GetComponent<Monster>(),GameManager.getInstance().Player_Object.transform.position))
			{
				return true;
			}
		}
			
		return false;
	}
	public void CollisionUpdate()
	{

	}
	bool CrashCheck(Monster monster_script , Vector3 _pos)
	{

//		if( Vector3.Dot(Player_Forward,monster_script.transform.position - _pos) > 0.0f)
//		{
			if(Vector3.Distance(_pos, monster_script.transform.position)
				< GameManager.getInstance().Player_script.player_data_.Player_Attack_Range)
			{
				return true;
			}
//		}
		
		return false;
	}
	bool CrashProtectionObject(float _Damage)
	{
		PO_script.DamagedProtect(_Damage);
		return true;
	}
}

